home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / System 7.0 Samples / Edition Manager / AppleEventM.c next >
Encoding:
C/C++ Source or Header  |  1994-11-18  |  12.2 KB  |  291 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2.  *
  3.  *  Apple Developer Technical Support
  4.  *
  5.  *  AppleEvent specific routines
  6.  *
  7.  *  Program:    EditionSample
  8.  *  File:       AppleEventM.c - C Source
  9.  *
  10.  *  by:         C.K. Haun <TR>
  11.  *
  12.  *  Copyright © 1990 Apple Computer, Inc.
  13.  *  All rights reserved.
  14.  *
  15.  *------------------------------------------------------------------------------
  16.  * This file handles the generic AppleEvent handlers, as well as intialization
  17.  * and installing the handlers I'll need for this application, specifically the 
  18.  * Edition Manager event handlers
  19.  *----------------------------------------------------------------------------*/
  20.  
  21.  
  22. #define __AEM__
  23. /* temporary, until it gets into the equates */
  24. #define kAECreatePublisher 'cpub'
  25.  
  26. #pragma segment Main
  27. #pragma load "EdSampheaders"                                /* see the Buildheaders.c file */
  28.  
  29. #include "EdSampdefines.h"
  30. #include <SegLoad.h>
  31.  
  32. /* prototypes */
  33. void DoHighLevel(EventRecord AERecord);
  34. void InitAEStuff(void);
  35. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  36. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  37. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  38. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  39. Boolean MissedAnyParameters(AppleEvent *message);
  40. OSErr processOpenPrint(AppleEvent *messagein, Boolean printIt);
  41. OSErr GetSectionHandleFromEvent(AppleEvent *AEin, SectionHandle *theSection);
  42. extern void ShowMe(Str255 in, OSErr aevtErr);
  43.  
  44. /* external functions */
  45. extern OSErr PrepQuit();
  46. extern pascal OSErr AEReadSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  47. extern pascal OSErr AEWriteSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  48. extern pascal OSErr AEScrollSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  49. extern pascal OSErr AECancelSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  50. extern pascal OSErr AECreatePubHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  51. extern WindowPtr OpenFile(FSSpec *inSpec);
  52. extern void PrintIt(WindowPtr theWind);
  53. extern WindowPtr AddNewWindow(Boolean showIt);
  54. extern void ChangePlane(WindowPtr twindow);
  55. extern void CloseMyWindow(WindowPtr theClose);
  56.  
  57. /* DoHighLevel processes our high level events */
  58. void DoHighLevel(EventRecord AERecord)
  59. {
  60.     OSErr fred2;
  61.     fred2 = AEProcessAppleEvent(&AERecord);
  62.     if ((fred2 != userCanceledErr) && (fred2 != noErr) && (fred2 != errAEEventNotHandled))
  63.         ShowMe("\pAppleEvent Proccessing.", fred2);
  64.     /* if it was a userCanceledErr (from the quit routine) and the reply has been */
  65.     /* sent from there already */
  66.     /* if it's a errAEEventNotHandled, then someone sent us  an event we're not prepared for, */
  67.     /* that is not a reason to put up a dialog.  Since anyone can send us events, then */
  68.     /* we could get all kinds of stray stuff, so just ignore it. */
  69.     
  70. }
  71.  
  72. /* end DoHighLevel */
  73.  
  74. /* InitAEStuff checks to see if this machine has AppleEvents and 
  75. *   does our setup.
  76. *   If AppleEvents are not found, we alert and exit.
  77. *   This is also the place where all the handlers for AppleEvents we deal
  78. *   with are installed.  This includes the required AppleEvents, and the
  79. *   Edition Manager specific ones used in this app.
  80. */
  81. #pragma segment MyInit
  82. void InitAEStuff(void)
  83. {
  84.     extern Boolean gHasAppleEvents;
  85.     extern long aLong;
  86.     OSErr aevtErr = noErr;
  87.     aLong = 0;
  88.     /* Check this machine for AppleEvents.  If they are not here (ie not 7.0)
  89.     *   then we exit */
  90.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
  91.     /* The following series of calls installs all our AppleEvent Handlers.
  92.     *   These handlers are added to the application event handler list that 
  93.     *   the AppleEvent manager maintains.  So, whenever an AppleEvent happens
  94.     *   and we call AEProcessEvent, the AppleEvent manager will check our
  95.     *   list of handlers and dispatch to it if there is one.
  96.     */
  97.     if (gHasAppleEvents) {
  98.         if (aevtErr == noErr)
  99.             /* First the four required events, OAPP,ODOC,PDOC, and QUIT */
  100.             aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(AEOpenHandler), 0, false);
  101.         else
  102.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  103.         if (aevtErr == noErr)
  104.             aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(AEOpenDocHandler), 0, false);
  105.         else
  106.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  107.         if (aevtErr == noErr)
  108.             aevtErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(AEPrintHandler), 0, false);
  109.         else
  110.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  111.         if (aevtErr == noErr)
  112.             aevtErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(AEQuitHandler), 0, false);
  113.         else
  114.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  115.         /* Now the Edition Manager events */
  116.         if (aevtErr == noErr)
  117.             aevtErr = AEInstallEventHandler(kCoreEventClass, kAECreatePublisher, NewAEEventHandlerProc(AECreatePubHandler), 0, false);
  118.  
  119.         if (aevtErr == noErr)
  120.             aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionReadMsgID, NewAEEventHandlerProc(AEReadSectionHandler), 0, false);
  121.         else
  122.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  123.         if (aevtErr == noErr)
  124.             aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionWriteMsgID, NewAEEventHandlerProc(AEWriteSectionHandler), 0, false);
  125.         else
  126.             ShowMe("\pinstall", aevtErr);
  127.         if (aevtErr == noErr)
  128.             aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionScrollMsgID, NewAEEventHandlerProc(AEScrollSectionHandler), 0, false);
  129.         else
  130.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  131.         if (aevtErr == noErr)
  132.             aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionCancelMsgID, NewAEEventHandlerProc(AECancelSectionHandler), 0, false);
  133.         else
  134.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  135.     } else {
  136.         Alert(kNoAppleEvents, nil);
  137.         ExitToShell();
  138.     }
  139. }
  140.  
  141. /* end InitAEStuff */
  142. #pragma segment Main
  143. /***************************************************************************************
  144.  
  145.     MissedAnyParameters
  146.  
  147.     Used to check for any unread required parameters. Returns true if we missed at
  148.     least one.
  149.  
  150. *****************************************************************************************/
  151. Boolean MissedAnyParameters(AppleEvent *message)
  152. {
  153.     OSErr err;
  154.     DescType ignoredActualType;
  155.     AEKeyword missedKeyword;
  156.     Size ignoredActualSize;
  157.     EventRecord event;
  158.     
  159.     err = AEGetAttributePtr(message, keyMissedKeywordAttr, typeKeyword, &ignoredActualType, (Ptr)&missedKeyword,
  160.                             sizeof(missedKeyword), &ignoredActualSize);
  161.     
  162.     /* no error means that we found some more.*/
  163.     
  164.     if (err == noErr) {
  165.         event.message = *(long *)&ignoredActualType;
  166.         event.where = *(Point *)&missedKeyword;
  167.         ShowMe("\pMissedAnyParameters: got parameters I don't know what to do with.", err);
  168.         err = errAEEventNotHandled;
  169.     }
  170.     
  171.     /* errAEDescNotFound means that there are no more parameters. If we get */
  172.     /* an error code other than that, flag it. */
  173.     
  174.     else if (err != errAEDescNotFound) {
  175.         ShowMe("\pMissedAnyParameters: after AEGetAttributeDesc.", err);
  176.     }
  177.     return(err != errAEDescNotFound);
  178. }
  179.  
  180. /* This is the standard Open Application event.  You'll get this as one of the (if not the ) */
  181. /* first events in your application.  So, we open up a blank document */
  182. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  183. {
  184. #pragma unused (messagein,reply,refIn)
  185.     ChangePlane(AddNewWindow(true));                        /* open our initial, blank window */
  186.     /* and make sure it's the front window */
  187.     return(noErr);
  188. }
  189.  
  190. /* end AEOpenHandler */
  191.  
  192. /* Open Doc, opens our documents.  Remember, this can happen at application start AND */
  193. /* anytime else.  If your app is up and running and the user goes to the desktop, hilites one */
  194. /* of your files, and double-clicks or selects Open from the finder File menu this event */
  195. /* handler will get called. Which means you don't do any initialization of globals here, or */
  196. /* anything else except open then doc.  */
  197. /* SO-- Do NOT assume that you are at app start time in this */
  198. /* routine, or bad things will surely happen to you. */
  199.  
  200. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  201. {
  202. #pragma unused (reply, refIn)
  203.     processOpenPrint(messagein, false);
  204. }
  205.  
  206. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  207. {                                                           /* no printing handler in yet, so we'll ignore this */
  208.     /* the operation is functionally identical to the ODOC event, with the additon */
  209.     /* of calling your print routine.  */
  210. #pragma unused (reply,refIn)
  211.     processOpenPrint(messagein, true);
  212.     return(noErr);
  213. }
  214.  
  215. /* Standard Quit event handler, to handle a Quit event from the Finder, for example.  */
  216. /* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life.  */
  217. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  218. {
  219. #pragma unused (messagein,refIn)
  220.     OSErr theErr;
  221.     Str32 userCanx = "\pUser canceled";
  222.     theErr = PrepQuit();                                    /* prepQuit sets the Stop flag for us.  It does _NOT_ quit, you */
  223.     /* should NEVER quit from an AppleEvent handler.  Calling */
  224.     /* ExitToShell here would blow things up */
  225.     if (theErr == noErr)
  226.         return(noErr);
  227.     if (theErr == (userCanceledErr)) {
  228.         /* reply to the application that told us to quit */
  229.         AEPutParamPtr(reply, 'errn', typeLongInteger, (Ptr)&theErr, sizeof(OSErr));
  230.         AEPutParamPtr(reply, 'errs', typeChar, (Ptr)userCanx, userCanx[0]);
  231.     } else
  232.         return(theErr);
  233.     
  234. }
  235.  
  236. /* my routine to snatch a section handle out of an AppleEvent */
  237. OSErr GetSectionHandleFromEvent(AppleEvent *AEin, SectionHandle *theSection)
  238. {
  239.     DescType thisType;
  240.     Size returnedSize;
  241.     return(AEGetParamPtr(AEin, keyDirectObject, typeSectionH, &thisType, (Ptr)theSection, sizeof(theSection), &returnedSize));
  242. }
  243.  
  244. /* processOpenPrint handles ODOC and PDOC events.  Both events open a document, one prints it */
  245. OSErr processOpenPrint(AppleEvent *messagein, Boolean printIt)
  246. {
  247.     OSErr err;
  248.     OSErr err2;
  249.     AEDesc theDesc;
  250.     FSSpec theFSS;
  251.     short loopy;
  252.     long numFilesToOpen;
  253.     AEKeyword ignoredKeyWord;
  254.     DescType ignoredType;
  255.     Size ignoredSize;
  256.     WindowPtr tWind;
  257.     err = AEGetParamDesc(messagein, keyDirectObject, typeAEList, &theDesc);
  258.     if (err) {
  259.         ShowMe("\pAEGetParamDesc.", err);
  260.     }
  261.     if (!MissedAnyParameters(messagein)) {
  262.         
  263.         /* Got all the parameters we need. Now, go through the direct object, */
  264.         /* see what type it is, and parse it up. */
  265.         
  266.         if (err = AECountItems(&theDesc, &numFilesToOpen))
  267.             ShowMe("\pAECountItems.", err);
  268.         else {
  269.             for (loopy = 1; ((loopy <= numFilesToOpen) && (!err)); ++loopy) {
  270.                 if (err = AEGetNthPtr(&theDesc, loopy, typeFSS, &ignoredKeyWord, &ignoredType, (Ptr)&theFSS, sizeof(theFSS),
  271.                                       &ignoredSize))
  272.                     ShowMe("\pAEGetNthDesc.", err);
  273.                 else
  274.                     tWind = OpenFile(&theFSS);
  275.                 if (printIt && tWind != nil) {
  276.                     
  277.                     PrintIt(tWind);                         /* in Print.c.  Does not yet print, but the idea is there */
  278.                     CloseMyWindow(tWind);
  279.                 }
  280.             }                                               /* for loopy = ... */
  281.         }                                                   /* AECountItems OK */
  282.     }                                                       /* Got all necessary parameters */
  283.     
  284.     if (err2 = AEDisposeDesc(&theDesc))
  285.         ShowMe("\pAEDisposeDesc of theDesc.", err2);
  286.     return(err ? err : err2);
  287. }
  288.  
  289.  
  290. #undef __AEM__
  291.